/*
 * @Author: llch
 * @Date: 2020-06-01 15:26:27
 * @LastEditTime: 2020-06-08 08:42:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \idiom_demo\assets\script\game\Grid.ts
 */ 

const {ccclass, property} = cc._decorator;

@ccclass
export class Grid extends cc.Component {

    public gridId: number = 0;                  // 格子id
    public isUsed: boolean = false;             // 是否是被使用的格子
    public data: string = null;                 // 完整的成语
    public char: string = "";                   // 使用的成语字符，单个字
    public idioms = [];                         // 反向保存idiom引用    这个是用来保存这个格子被哪些词条引用
    public isShareGrid: boolean = false;        // 是否是 共享 格子
    public isSpaceGrid: boolean = false;        // 是否是 被 去字 状态
    public isSelectMode: boolean = false;       // 是否是 填字 模式
    public isSuccess: boolean = false;          // 标注该位置词 是否正确
    public currentId: number = 0;               // 记录当前格子上 选词 的id，用来判断回退

    @property(cc.Sprite)
    public bg: cc.Sprite = null;
    @property(cc.Label)
    public word: cc.Label = null;

    @property({     // 正常状态
        type: cc.SpriteFrame,
    })
    private word_normal: cc.SpriteFrame = null;

    onLoad() {
        // 初始化
        this.gridId = 0;
        this.isUsed = false
        this.data = null;
        this.char = "";
        this.idioms = [];
        this.isShareGrid = false;
        this.isSpaceGrid = false;
        this.isSelectMode = false;
        this.isSuccess = false;
        this.currentId = 0;
    }

    public resetGrid() {
        // 重置
        this.gridId = 0;
        this.isUsed = false
        this.data = null;
        this.char = "";
        this.idioms = [];
        this.isShareGrid = false;
        this.isSpaceGrid = false;
        this.isSelectMode = false;
        this.isSuccess = false;
        this.currentId = 0;

        this.bg.spriteFrame = this.word_normal;
        this.word.string = "";
        this.word.node.color = cc.Color.BLACK;

        this.hide();
    }

    public show() {
        this.bg.node.active = true;
        this.word.node.active = true;
    }

    public hide() {
        this.bg.node.active = false;
        this.word.node.active = false;
    }

    public addListener(fn: Function, target) {
        this.node.on(cc.Node.EventType.TOUCH_END, fn, target);
    }

    public removeListener(fn: Function, target) {
        this.node.off(cc.Node.EventType.TOUCH_END, fn, target);
    }

} 